package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.CourseDao;
import com.gdit.zax.dao.SearchDao;
import com.gdit.zax.entitys.Course;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;

public class SearchCourse extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String key = request.getParameter("key");

		// 检测参数
//		if (TextUtil.isEmpty(accesstoken)) {
//			O.printFail(response, 1, "请检查accesstoken参数!");
//			return;
//		}

		// 检测连接、用户
		Connection conn = DBUtil.getConnection();

		if (conn == null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}

		// 开始
		DAOFactory dao = DAOFactory.init(conn, request, response);
		SearchDao sdao = dao.getSearchDao();
		sdao.addSearchRecod(0, 2, key);//添加搜索记录
		
		ArrayList<Course> list = sdao.sreachCrouse(key, 1, 1000);
		ArrayList<Map<String, Object>> out =new ArrayList<Map<String,Object>>();
		
		for (Course course : list) {
			Map<String, Object> map =new HashMap<String, Object>();
			map.put("imageurl", Constant.WEBRoot +  course.getPic());
			map.put("id", course.getId());
			map.put("time", course.getAddtime());
			map.put("name", course.getName());
			map.put("introduction", course.getIntroduction());
			map.put("anthorname", course.getAnthorid());
			map.put("seesum", course.getSeesum());
			map.put("collectsum", course.getCollectsum());
			map.put("dynamic", course.getDynamic());
			map.put("commentsum", course.getCommentsum());
			out.add(map);
		}
		O.printSuccess(response, out);
	}

}
